#pragma once

#include "requestor.hpp"
#include <unordered_set>
#include "../common/uuid.hpp"
namespace rpc
{
    namespace client
    {
        /*提供注册服务接口*/
        class Provider
        {
        public:
            using ptr = std::shared_ptr<Provider>;
            Provider(const Requestor::ptr &requestor) : _requestor(requestor) {}
            bool registryMethod(const BaseConnection::ptr &conn, const std::string &method, const Address &host)
            {
                auto msg_req = MessageFactory::create<ServiceRequest>();

                msg_req->setId(UUID::uuid());
                msg_req->setMType(MType::REQ_SERVICE);
                msg_req->setMethod(method);
                msg_req->setHost(host);
                msg_req->setOptype(ServiceOptype::SERVICE_REGISTRY);
                BaseMessage::ptr msg_rsp;
                _requestor->send(conn, msg_req, msg_rsp);
                auto service_rsp = std::dynamic_pointer_cast<ServiceResponse>(msg_rsp);
                if (service_rsp.get() == nullptr)
                {
                    LOG(ERROR, "响应类型向下转换失败！");
                    return false;
                }
                if (service_rsp->rcode() != RCode::RCODE_OK)
                {
                    LOG(ERROR, "服务注册失败，原因：%s", errReason(service_rsp->rcode()).c_str());
                    return false;
                }
                return true;
            }

        private:
            Requestor::ptr _requestor;
        };

        /*
            增加主机
            删除主机
            选择主机 （负载均衡）
        */
        class MethodHost
        {
        public:
            using ptr = std::shared_ptr<MethodHost>;
            MethodHost() : _idx(0) {}
            MethodHost(const std::vector<Address> &hosts) : _hosts(hosts.begin(), hosts.end()), _idx(0) {}
            // 中途收到了服务上线请求后被调用
            void appendHost(const Address &host)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                _hosts.emplace_back(host);
            }

            // 中途收到了服务下线请求后被调用
            void removeHost(const Address &host)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                for (auto it = _hosts.begin(); it != _hosts.end(); ++it)
                {
                    if (*it == host)
                    {
                        _hosts.erase(it);
                        break;
                    }
                }
            }
            Address chooseHost()
            {
                std::unique_lock<std::mutex> lock(_mutex);
                auto pos = (_idx++) % _hosts.size();
                return _hosts[pos];
            }
            bool empty()
            {
                std::unique_lock<std::mutex> lock(_mutex);
                return _hosts.empty();
            }

        private:
            std::mutex _mutex;
            size_t _idx;
            std::vector<Address> _hosts;
        };

        class Discoverer
        {
        public:
            using OfflineCallback = std::function<void(const Address &)>;
            using ptr = std::shared_ptr<Discoverer>;
            Discoverer(const Requestor::ptr &requestor, const OfflineCallback &cb) : _requestor(requestor),
                                                                                     _offline_callback(cb) {}

            // 返回提供服务主机功能
            bool serviceDiscovery(const BaseConnection::ptr &conn, const std::string &method, Address &host)
            {
                {
                    // 1.当前所保管的提供者信息存在，则直接返回地址
                    std::unique_lock<std::mutex> lock(_mutex);
                    auto it = _method_hosts.find(method);
                    if (it != _method_hosts.end())
                    {
                        if (it->second->empty() == false)
                        {
                            host = it->second->chooseHost();
                            return true;
                        }
                    }
                }

                // 2.不存在提供该服务的主机
                // 进行服务发现
                auto msg_req = MessageFactory::create<ServiceRequest>();
                msg_req->setId(UUID::uuid());
                msg_req->setMType(MType::REQ_SERVICE);
                msg_req->setMethod(method);
                msg_req->setOptype(ServiceOptype::SERVICE_DISCOVERY);
                BaseMessage::ptr msg_rsp;
                _requestor->send(conn, msg_req, msg_rsp);

                auto service_rsp = std::dynamic_pointer_cast<ServiceResponse>(msg_rsp);
                if (!service_rsp)
                {
                    LOG(ERROR, "服务发现失败！响应类型转换失败！");
                    return false;
                }
                if (service_rsp->rcode() != RCode::RCODE_OK)
                {
                    LOG(ERROR, "服务发现失败！%s", errReason(service_rsp->rcode()).c_str());
                    return false;
                }

                // 判断是否发现成功
                std::unique_lock<std::mutex> lock(_mutex);
                auto method_host = std::make_shared<MethodHost>(service_rsp->Host());
                if (method_host->empty())
                {
                    LOG(ERROR, "%s 服务发现失败！没有能够提供服务的主机！", method.c_str());
                    return false;
                }
                host = method_host->chooseHost();
                // 管理起来
                _method_hosts[method] = method_host;
                return true;
            }

            // 这个接口是提供给Dispatcher模块进行服务上线下线请求处理的回调函数
            void onServiceRequest(const BaseConnection::ptr &conn, const ServiceRequest::ptr &msg)
            {
                auto optype = msg->optype();
                std::string method = msg->method();
                std::unique_lock<std::mutex> lock(_mutex);
                if (optype == rpc::ServiceOptype::SERVICE_ONLINE)
                {
                    // 判断是否是第一次添加
                    auto it = _method_hosts.find(method);
                    if (it == _method_hosts.end())
                    {
                        MethodHost::ptr method_host = std::make_shared<MethodHost>();
                        _method_hosts[method] = method_host;
                    }
                    else
                    {
                        it->second->appendHost(msg->host());
                    }
                }
                else if (optype == rpc::ServiceOptype::SERVICE_OFFLINE)
                {
                    // 差错处理，可能不存在该服务
                    auto it = _method_hosts.find(method);
                    if (it == _method_hosts.end())
                    {
                        return;
                    }
                    it->second->removeHost(msg->host());
                    _offline_callback(msg->host());
                }
            }

        private:
            OfflineCallback _offline_callback;
            std::mutex _mutex;
            std::unordered_map<std::string, MethodHost::ptr> _method_hosts;
            Requestor::ptr _requestor;
        };
    }
}